### This script contains functions that prepare maps (that contain all years) and clean maps by individual years.

prepare_maps <- function(country, years = NULL, alt = F) {
  ### Load codebook maps
  varname <- read.csv(sprintf("source/raw/codebook_map/varname/%s.csv", country), encoding = "UTF-8")
  party   <- read.csv(sprintf("source/raw/codebook_map/party/%s.csv",   country), encoding = "UTF-8")
  thermo  <- read.csv(sprintf("source/raw/codebook_map/thermo/%s.csv",  country), encoding = "UTF-8")
  ideo    <- read.csv(sprintf("source/raw/codebook_map/ideo/%s.csv",    country), encoding = "UTF-8")
  
  ### Extract variables
  # years
  if (is.null(years)) years <- extract_years(varname, country, alt)
  # format
  if ("format"  %in% colnames(varname)) {
    format <- varname$format
  } else {
    format <- rep("sav", length(years))
  }
  
  ### Class types
  varname <- varname  %>%  mutate_all(as.character)
  party   <- party    %>%  mutate_all(as.character)
  thermo  <- thermo   %>%  mutate_all(as.character)
  ideo    <- ideo     %>%  mutate_all(as.character)
  
  return(list(years = years, format = format, varname = varname, party = party, thermo = thermo, ideo = ideo))
}

extract_years <- function(df, country, alt = F) {
  # Extract all years from the data frame
  if (country == "germany") {
    if (alt){
      years   <- c(c(1990:1995), c(1999:2018))
    } else{
      years   <- c(1977:2018)
    }
    
  } else if (country == "switzerland") {
    years   <- c(1975, 1995, 1999, 2007, 2011)
    
  } else if (country == "united_states") {
    years   <- c(1978, 1980, 1982, 1984, 1986, 1988, 1990, 1992, 1994, 
                 1996, 1998, 2000, 2002, 2004, 2008, 2012, 2016, 2020)
    
  } else if (country == "cses") {
    years   <- c(1996:2019)
  } else {
    years_temp <- df$year  %>%
      as.character()
    years <- NULL
    for (i in 1:length(years_temp)) {
      if (str_length(years_temp[i]) == 4) {
        yr <- years_temp[i]
      } else {
        yr <- str_split(years_temp[i], ",")  %>%
          unlist()
      }
      years <- c(years, yr)
      years <- gsub(" ", "", years, fixed = T)  %>%
        as.numeric()
    }
    years <- years  %>%
      sort()
  }
  return(years)
}

clean_maps_by_year <- function(country, yr, maps) {
  varname <- maps$varname
  party   <- maps$party
  thermo  <- maps$thermo
  ideo    <- maps$ideo
  
  varname_yr        <- varname[grep(sprintf("%s|all", yr), varname$year), ]
  parties_yr        <- party  [grep(sprintf("%s|all", yr), party$year),   ]  %>%  filter(variable == "parties")
  thermo_labs_yr    <- thermo [grep(sprintf("%s|all", yr), thermo$year),  ]  %>%  filter(variable == "thermometers")
  thermo_vals_yr    <- thermo [grep(sprintf("%s|all", yr), thermo$year),  ]  %>%  filter(variable == "thermo_vals")
  ideo_labs_yr      <- ideo [grep(sprintf("%s|all", yr), ideo$year),  ]  %>%  filter(variable == "ideo_labs")
  ideo_vals_yr      <- ideo [grep(sprintf("%s|all", yr), ideo$year),  ]  %>%  filter(variable == "ideo_vals")
  
  if (country == "germany") {
    thermo_vars_yr  <- party  %>%  filter(year == ifelse(yr %in% c(1977:2012), "1977-2012", "2013-2018"), variable == "thermo_vars")
  } else {
    thermo_vars_yr  <- party  [grep(sprintf("%s|all", yr), party$year),   ]  %>%  filter(variable == "thermo_vars")
  }

  
  if (country == "canada" & yr == 2019){ # Need to specify in R because of string type issues
    parties_yr[7] <- "(4) Bloc Qu\xe9b\xe9cois (BQ, PQ, Bloc, Parti Qu\xe9b\xe9cois)"
  }
  
  return(list(varname_yr      = varname_yr,
              parties_yr      = parties_yr,
              thermo_vars_yr  = thermo_vars_yr,
              thermo_labs_yr  = thermo_labs_yr,
              thermo_vals_yr  = thermo_vals_yr,
              ideo_labs_yr    = ideo_labs_yr,
              ideo_vals_yr    = ideo_vals_yr
              ))
}
